<template>
  <n-modal
    v-model:show="showModal"
    :show-icon="false"
    preset="dialog"
    :on-after-leave="cancel"
    title="重置密码"
  >
    <n-form
      :model="formParams"
      :rules="rules"
      ref="formRef"
      label-placement="left"
      :label-width="80"
      class="py-4"
    >
      <n-form-item label="用户编号" path="userCode">
        <n-input placeholder="请输入用户编号" v-model:value="formParams.userCode" />
      </n-form-item>
      <n-form-item label="用户名称" path="userName">
        <n-input placeholder="请输入用户名称" v-model:value="formParams.userName" />
      </n-form-item>
      <n-form-item label="真实名称" path="realName">
        <n-input placeholder="请输入真实名称" v-model:value="formParams.realName" />
      </n-form-item>
      <n-form-item label="密码" path="password">
        <n-input placeholder="请输入密码" v-model:value="formParams.password" />
      </n-form-item>
    </n-form>

    <template #action>
      <n-space>
        <n-button @click="cancel">取消</n-button>
        <n-button type="info" :loading="formBtnLoading" @click="confirmForm">确定</n-button>
      </n-space>
    </template>
  </n-modal>
</template>

<script lang="ts" setup>
import { ref, defineExpose, reactive, defineEmits } from 'vue';
import { resetPassword } from '@/api/system/user';
import { useMessage } from 'naive-ui';
import commonUtils from '@/utils/commonUtils';

const formRef: any = ref(null);
const showModal = ref(false);
const formBtnLoading = ref(false);
const message = useMessage();
let _clickData = null;
let isUpdate = ref(false);

const defaultValueRef = () => ({
  userId: null,
  userCode: '',
  userName: '',
  realName: null,
  password: null,
});

const formParams = reactive(defaultValueRef());

const rules = {
  password: {
    required: true,
    trigger: ['blur', 'change'],
    message: '请选择密码',
  },
};

function open(clickData = null) {
  if (clickData != null) {
    Object.assign(formParams, clickData);
    formParams.password = '123456';
  }
  showModal.value = true;
}

//取消
const cancel = () => {
  Object.assign(formParams, defaultValueRef());
  showModal.value = false;
};

function confirmForm(e) {
  e.preventDefault();
  formBtnLoading.value = true;
  formRef.value.validate((errors) => {
    if (!errors) {
      resetPassword(commonUtils.getformNoNullParams(formParams)).then((res) => {
        if (res.success) {
          message.success('重置成功');
          showModal.value = false;
        }
      });
    } else {
      message.error('请填写完整信息');
    }
    formBtnLoading.value = false;
  });
}

defineExpose({
  open,
});
</script>

<style>
</style>